package com.article.controller;


import com.article.entity.dto.AdminDao;
import com.article.entity.dto.AdminSearchDao;
import com.article.entity.pojo.Admin;
import com.article.entity.vo.AdminVo;
import com.article.result.Result;
import com.article.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin")
public class AdminController {


    private static final Logger log = LoggerFactory.getLogger(AdminController.class);
    @Autowired
    private AdminService adminService;

    @PostMapping()
    public Result getAdminPage(@RequestBody AdminSearchDao adminSearchDao) {
        log.info("搜索的adminSearchDao:{}", adminSearchDao);
        AdminVo result = adminService.getAdminPage(adminSearchDao);
        return Result.success(result);

    }

    @PostMapping("/add")
    public Result addAdmin(@RequestBody AdminDao adminDao) {
        log.info("添加的adminDao:{}", adminDao);
        if (adminService.addAdmin(adminDao)) {
            return Result.success();
        }
        return Result.error("添加失败");
    }

    @PostMapping("/update")
    public Result updateAdmin(@RequestBody AdminDao adminDao) {
        log.info("更新的adminDao:{}", adminDao);
        if (adminService.updateAdmin(adminDao)) {
            return Result.success();
        }
        return Result.error("更新失败");
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteAdmin(@PathVariable("id") Integer id) {
        log.info("删除的id:{}", id);
        if (adminService.deleteAdmin(id)) {
            return Result.success();
        }
        return Result.error("删除失败");
    }

    @DeleteMapping("/deletes")
    public Result deleteAdmins(@RequestBody Integer[] ids) {
        log.info("删除的ids:{}", ids);
        if (adminService.deleteAdmins(ids)) {
            return Result.success();
        }
        return Result.error("删除失败");
    }
}
